package com.forte.shoppingcustomerdao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

//import com.forte.cartdao.Cart;

@Transactional
@Repository("customerDao")
public class ShoppingCustomerDAOImpl implements ShoppingCustomerDAO  {
	
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	
	protected final Session getCurrentSession(){
		return sessionFactory.getCurrentSession();
	}

	public void saveOrUpdate(ShoppingCustomer customer) {
		getCurrentSession().saveOrUpdate(customer);		
	}

	public void saveOrUpdate(Address address) {
		getCurrentSession().saveOrUpdate(address);		
	}

//	public void saveOrUpdate(Cart cart) {
//		getCurrentSession().saveOrUpdate(cart);		
//	}
	
	public ShoppingCustomer findCustomerByCustomerId(int customerId){
		Query  query = getCurrentSession().createQuery(
				"from shoppingcustomer where customerId  = :customerId");
		query.setParameter("customerId", customerId);
		return foundCustomer(query.list());
	}
	
	@SuppressWarnings("rawtypes")
	public ShoppingCustomer foundCustomer(List list){
		ShoppingCustomer customerFound = null;
		if (list.size() > 0){
			customerFound =  (ShoppingCustomer) list.get(0);
		}
		return customerFound;
	}
	
	public ShoppingCustomer findCustomerByEmailAndPassword(String emailAddress, String password){
		Query query = getCurrentSession().createQuery(
				"from shoppingcustomer where emailAddress = :emailAddress and password = :password");
		query.setParameter("emailAddress", emailAddress);
		query.setParameter("password", password);
		
		return foundCustomer(query.list());
	}

	public ShoppingCustomer findCustomerByEmail(String emailAddress) {
		Query query = getCurrentSession().createQuery(
				"from shoppingcustomer where emailAddress = :emailAddress");
		query.setParameter("emailAddress", emailAddress);

		return foundCustomer(query.list());
	}
	
	@SuppressWarnings("unchecked")
	public List<Address> findAllAddress() {
		Query query = getCurrentSession().createQuery("from address ");
		
		return (List<Address>) query.list() ; 
	}

	public Address findAddressByAddressId(int addressId) {
		Query query = getCurrentSession().createQuery("from address where addressId = :addressId ");
		query.setParameter("addressId", addressId);
		return foundAddress(query.list());
	}
	
	@SuppressWarnings("rawtypes")
	public Address foundAddress(List list){
		Address addressFound = null;
		if (list.size() > 0){
			addressFound = (Address) list.get(0);
		}
		return addressFound;		
	}
	
	public Address findaddressByUserIdAndAddressType(int customerId, String addressType){
		Query query = getCurrentSession().createQuery(				
				"from address where customerId = :customerId and addressType = :addressType ");
		query.setParameter("customerId", customerId);
		query.setParameter("addressType", addressType);
		
		return foundAddress(query.list());
	}
	

	
}